package net.dao;

import java.util.HashMap;
import java.util.Map;

public class DAOThreadVariable {

	public static final String SCHEMA = "schema";
	
	public static final String DB_TYPE = "db_type";
	
    private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();


    /**
     * 获得线程中保存的属性.
     *
     * @param attribute 属性名称
     * @return 属性值
     */
    public static Object get(String attribute) {
        Map map = (Map) threadLocal.get();
        if (map == null) {
            return null;
        }
        return map.get(attribute);
    }


    /**
     * report
     * 获得线程中保存的用户信息.
     */
    public static String getScheam() {
        Map map = (Map) threadLocal.get();
        if (map == null) {
            return null;
        }
        Object obj = map.get(SCHEMA);

        if (obj != null) {
            return (String) obj;
        }
        return null;
    }


    public static void setScheam(String schema) {
        Map map = (Map) threadLocal.get();

        if (map == null) {
            map = new HashMap();
            threadLocal.set(map);
        }
        map.put(SCHEMA, schema);
    }
    
    /**
     * report
     * 获得线程中保存的数据库类型信息.
     */
    public static String getDBType() {
        Map map = (Map) threadLocal.get();
        if (map == null) {
            return null;
        }
        Object obj = map.get(DB_TYPE);

        if (obj != null) {
            return (String) obj;
        }
        return null;
    }


    public static void setDBType(String dbType) {
        Map map = (Map) threadLocal.get();

        if (map == null) {
            map = new HashMap();
            threadLocal.set(map);
        }
        map.put(DB_TYPE, dbType);
    }


    /**
     * 清除线程中保存的数据
     */
    public static void clearThreadVariable() {
        threadLocal.remove();
    }
}
